<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>user_operate</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <style>
        .view-form .layui-input-block {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>
<div style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">登录名：</label>
            <div class="layui-input-block">
                <input type="text" name="USERNAME" lay-verify="required" placeholder="请输入登录名" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>
            <div class="layui-input-block">
                <input type="password" name="PASSWORD" style="display:none">
                <input type="password" name="PASSWORD"   id="PASSWORD" placeholder="请输入密码" autocomplete="off"
                       class="layui-input" readonly onfocus="this.removeAttribute('readonly');"
                >

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色：</label>
            <div class="layui-input-block">
                <select name="TB_ROLE_ID" lay-verify="required" class="layui-select">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称：</label>
            <div class="layui-input-block">
                <input type="text" name="NAME" lay-verify="required" placeholder="请输入昵称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式：</label>
            <div class="layui-input-block">
                <input type="tel" name="PHONE" placeholder="请输入联系方式" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电子邮箱：</label>
            <div class="layui-input-block">
                <input type="email" name="EMAIL" placeholder="请输入电子邮箱" autocomplete="off"
                       class="layui-input" id="email">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <textarea name="BZ" placeholder="请输入备注" autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="user">保存</button>
                <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    layui.use(['jquery', 'form'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery;

        //操作类型：add新增，edit修改，view查看
        var operateType = QueryUtils.GetQueryString("type");
        var id = QueryUtils.GetQueryString("id");

        var active = {
            // 初始化角色下拉框
            initRoleSelect: function () {
                $.ajax({
                    url: "/shsRole/list",
                    data: null,
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
//                           console.log(res.data);
                            if (!!res.data) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var d = res.data[i];
                                    var str = '<option value="' + d.ID + '">' + d.ROLE_NAME + '</option>';
                                    $(".layui-select").append(str);
                                }
                                form.render();
                            }
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            }
        };

        active.initRoleSelect();

        switch (operateType) {
            case "add":
                break;
            case "edit":
                $.ajax({
                    url: "/user/info",
                    data: {USER_ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $("input[name='USERNAME']").val(res.data.USERNAME);
                            $("input[name='USERNAME']").attr("readonly", "readonly");
                            // $("input[name='PASSWORD']").parent().parent().remove();
                            $("select[name='TB_ROLE_ID']").val(res.data.TB_ROLE_ID);
                            $("input[name='NAME']").val(res.data.NAME);
                            $("input[name='PHONE']").val(res.data.PHONE);
                            $("input[name='EMAIL']").val(res.data.EMAIL);
                            $("textarea[name='BZ']").val(res.data.BZ);

                            $("#PASSWORD").attr("placeholder","密码为空代表不更改");
                            form.render();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }

        //监听提交
        form.on('submit(user)', function (data) {
            var email = $("#email").val();
            if (!!email && !email.match(("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"))) {
                layer.tips('邮箱格式不正确', '#email', {tips: 2});
                $("#email").focus();
                return false;
            }
            if (operateType === "add") {
                /*layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                });*/
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/user/save",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建商圈页面
                                    parent.layui.table.reload('userTable', {});    // 父页面表格刷新
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            } else if (operateType === "edit") {
                data.field.USER_ID = id;
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/user/edit",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭修改商圈页面
                                    parent.reloadTable();    // 父页面表格刷新
                                    parent.layer.msg("修改成功", {offset: '150px'});   // 父页面弹出修改成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });

    });
</script>
</body>
</html>